2 // ShareeSuggestionsDataSource.swift
5 // Created by Claudio Cambra on 2/4/24.
11 import SuggestionsTextFieldKit
13 class ShareeSuggestionsDataSource: SuggestionsDataSource {
15 var suggestions: [Suggestion] = []
16 var inputString: String = "" {
17 didSet { Task { await updateSuggestions() } }
20 init(kit: NextcloudKit) {
24 private func updateSuggestions() async {
25 let sharees = await fetchSharees(search: inputString)
26 Logger.shareeDataSource.info("Fetched \(sharees.count, privacy: .public) sharees.")
27 suggestions = suggestionsFromSharees(sharees)
28 NotificationCenter.default.post(name: SuggestionsChangedNotificationName, object: self)
31 private func fetchSharees(search: String) async -> [NKSharee] {
32 Logger.shareeDataSource.debug("Searching sharees with: \(search, privacy: .public)")
33 return await withCheckedContinuation { continuation in
38 completion: { account, sharees, data, error in
39 defer { continuation.resume(returning: sharees ?? []) }
40 guard error == .success else {
41 Logger.shareeDataSource.error(
42 "Error fetching sharees: \(error.description, privacy: .public)"
51 private func suggestionsFromSharees(_ sharees: [NKSharee]) -> [Suggestion] {
54 imageName: "person.fill",
55 displayText: $0.label.isEmpty ? $0.name : $0.label,